package base

import (
	"gitee.com/gopher2011/go-zero-gva/internal/svc"
	"gitee.com/gopher2011/go-zero-gva/internal/types"
	"github.com/mojocn/base64Captcha"
)

func (l *Base) Captcha(req types.Request) (*types.Response, error) {
	// 字符,公式,验证码配置
	// 生成默认数字的driver
	driver := base64Captcha.NewDriverDigit(l.SvcCtx.Config.Captcha.ImgHeight, l.SvcCtx.Config.Captcha.ImgWidth, l.SvcCtx.Config.Captcha.KeyLong, 0.7, 80)
	cp :=base64Captcha.NewCaptcha(driver,store)
	if id,b64s,err := cp.Generate();err !=nil {
		l.Logger.Error("验证码获取失败!",err)
		return &types.Response{Code: 7,Msg: "验证码获取失败"}, err
	}else {
		return &types.Response{Code:0, Msg: "验证码获取成功",Data: svc.SysCaptchaResp{
			CaptchaId: id,
			PicPath: b64s,
		}}, nil
	}
}

func (l *Base) Set(id string, value string){
	if err := l.SvcCtx.Cache.SetWithExpire(l.PreKey+id, value, l.Expiration);err !=nil {
		l.Logger.Error("RedisStoreSetError!")
	}
}

func (l *Base) Get(key string, clear bool) (value string){
	if err := l.SvcCtx.Cache.Get(key,&value);err !=nil {
		l.Logger.Error("RedisStoreGetError!")
		return ""
	}
	if clear {
		if err := l.SvcCtx.Cache.Del(key);err !=nil {
			l.Logger.Error("RedisStoreClearError!")
			return ""
		}
	}
	return
}

func (l *Base) Verify(id, answer string, clear bool) bool{
	return l.Get(l.PreKey + id,clear) == answer
}



